#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 24 déc. 2011

@author: nathanael
'''

import os
import re
import time

from pybible.data import DATA_PATH
from pybible.core.data import openBook
from pybible.core.find import FindKernel, RegexPattern

def xml2txt(src, dst):
    book = openBook(src)
    with open(dst, 'w') as fd:
        for e in book.get_part()._data._element.iter():
            if e.tag == 'verse':
                fd.write("%s\n" % e.text)
    
def find_xml(book, pattern):
    kernel = FindKernel(RegexPattern(pattern))
    return len(kernel.find(book.get_part()._data))
    
def find_txt(book, pattern):
    print pattern
    return len(re.findall(pattern, book.read()))
    
    
def bench(xml, txt):
    book_xml = openBook(xml)
    book_txt = open(txt)
    
    
    
    def bb(pattern):
        t1 = time.time()
        r1 = find_xml(book_xml, pattern)
        t2 = time.time() 
        r2 = find_txt(book_txt, pattern)
        t3 = time.time()
        print "bencf '%s'" % pattern
        print r1, r2
        print t2 - t1, t3 - t2 
    
    bb(RegexPattern.patternBuilderBylanguage(u'noé'))
    
    book_txt.close()
    
if __name__ == '__main__':
    bench(os.path.join(DATA_PATH, 'french_jn_darby.xml'),
          os.path.join(DATA_PATH, 'french_jn_darby.txt'))
